package callBack;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * <pre>
 * 功能描述：回调方法实现
 * 场景：同步回调的例子，意思是老师向Ricky问问题，Ricky给出答案，老师问下一个同学，得到答案之后继续问下一个同学
 * </pre>
 *
 * @author: 李明杰
 * @date: 2022/12/1 20:49
 */

public class AsyncCallback {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Jack());
        list.add(new Ricky());

        Teacher teacher = new Teacher(list);
        teacher.askQuestionAsync();
    }

    public interface Callback {

        public void tellAnswer(int answer);

    }

    /**
     * 学生接口，原文出处http://www.cnblogs.com/xrq730/p/6424471.html
     */
    public interface Student {

        public boolean resolveQuestion(Callback callback);

    }

    /**
     * 老师对象，原文出处http://www.cnblogs.com/xrq730/p/6424471.html
     */
    public static class Teacher implements Callback {

        private List<Student> students;

        public Teacher(List<Student> students) {
            this.students = students;
        }

        public void askQuestionAsync() throws ExecutionException, InterruptedException {
            System.out.println("请回答问题");
            ExecutorService pool = Executors.newFixedThreadPool(2);
            for (Student student : students) {
//                CompletableFuture<Boolean> futureResult = CompletableFuture.supplyAsync(() -> student.resolveQuestion(this), pool);
                //二选一
                Future<Boolean> futureResult = pool.submit(() -> student.resolveQuestion(this));
            }
        }


        @Override
        public void tellAnswer(int answer) {
            System.out.println("知道了，你的答案是" + answer);
        }

    }


    public static class Jack implements Student {

        @Override
        public boolean resolveQuestion(Callback callback) {
            // 模拟解决问题
            try {
                Thread.sleep(1000L);
                System.out.print(Thread.currentThread() + ": ");
                System.out.println(this.getClass().getSimpleName() + "正在回答问题");

            } catch (InterruptedException e) {

            }

            // 回调，问老师答案
            callback.tellAnswer(3);
            return true;
        }
    }

    /**
     * 一个名叫Ricky的同学解决老师提出的问题，原文出处http://www.cnblogs.com/xrq730/p/6424471.html
     */
    public static class Ricky implements Student {

        @Override
        public boolean resolveQuestion(Callback callback) {

            // 模拟解决问题
            try {
                Thread.sleep(1500L);
                System.out.print(Thread.currentThread() + ": ");
                System.out.println(this.getClass().getSimpleName() + "正在回答问题");
            } catch (InterruptedException e) {

            }

            // 回调，问老师答案
            callback.tellAnswer(2);
            return true;
        }


    }
}
